/**
 * Copyright 2009 by Nathaniel Harward.
 * 
 * This file is part of jtyrant-net.
 * 
 * jtyrant-net is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * jtyrant-net is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with jtyrant-net.  If not, see <http://www.gnu.org/licenses/>
 */
package us.harward.jtn;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Error codes and their messages, as determined by the Ruby API source code.
 */
public enum ErrorCode {

    ESUCCESS(0, "success"), EINVALID(1, "invalid operation"), ENOHOST(2,
	    "host not found"), EREFUSED(3, "connection refused"), ESEND(4,
	    "send error"), ERECV(5, "recv error"), EKEEP(6, "existing record"), ENOREC(
	    7, "no record found"), EMISC(9999, "miscellaneous error");

    private static Map<Integer, ErrorCode> lookupMap;

    static {
	final Map<Integer, ErrorCode> map = new HashMap<Integer, ErrorCode>();
	for (ErrorCode code : ErrorCode.values())
	    map.put(code.getCode(), code);
	lookupMap = Collections.unmodifiableMap(map);
    }

    private int code;
    private String message;

    private ErrorCode(final int code, final String message) {
	this.code = code;
	this.message = message;
    }

    public static ErrorCode lookup(final int code) {
	return lookupMap.get(code);
    }

    public int getCode() {
	return code;
    }

    @Override
    public String toString() {
	return message;
    }

}
